package com.aso114.project.adapter;

import android.view.View;
import android.widget.ImageView;

import com.aso114.project.R;
import com.aso114.project.base.ModuleExamBean;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;
import java.util.Locale;

/**
 * @author lin
 * @description: 章节练习适配器
 * @date 2019/3/11 0011
 */
public class ModuleExamAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_COMMON = 0;

    public ModuleExamAdapter() {
        super(null);
        addItemType(TYPE_COMMON, R.layout.item_module_exam);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        final ModuleExamBean moduleExamBean = (ModuleExamBean) item;
        helper.addOnClickListener(R.id.iv_edit);
        helper.setText(R.id.tv_exam_title, ((ModuleExamBean) item).getSpecialTitle());
        if (moduleExamBean.getRealAnswerCount() <= 0) {
            helper.setText(R.id.tv_rate, "正确率：0%");
        } else {
            float rate = moduleExamBean.getRealAnswerStateCount() * 100.0f / moduleExamBean.getRealAnswerCount();
            helper.setText(R.id.tv_rate, String.format(Locale.CHINA, "正确率：%.1f%%", rate));
        }
        helper.setText(R.id.tv_answer, String.format(Locale.CHINA, "已答题：%d/%d", moduleExamBean.getRealAnswerCount(), moduleExamBean.getTotalQuestions()));
        ImageView ivLevelIcon = helper.getView(R.id.iv_level_icon);
        View topLine = helper.getView(R.id.v_line_top);
        View bottomLine = helper.getView(R.id.v_line_bottom);
        final int pos = helper.getAdapterPosition();
        //使用修改过的层级区分
        switch (moduleExamBean.getLevel()) {
            default:
                break;
            case 0:
                helper.itemView.setBackgroundResource(R.color.white);
                ivLevelIcon.setImageResource(moduleExamBean.isExpanded() ? R.mipmap.ej_zk_ic :
                        R.mipmap.ej_yiji_ic);
                topLine.setVisibility(View.INVISIBLE);
                bottomLine.setVisibility(moduleExamBean.isExpanded() ? View.VISIBLE : View.INVISIBLE);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (moduleExamBean.isExpanded()) {
                            collapse(pos, false);
                        } else {
                            expand(pos, false);
                        }
                    }
                });
                List<ModuleExamBean> sub1Items = moduleExamBean.getSubItems();
                //标记子节点最后一个
                if (sub1Items != null && !sub1Items.isEmpty()) {
                    sub1Items.get(sub1Items.size() - 1).setLastIndex(true);
                }
                break;
            case 1:
                helper.itemView.setBackgroundResource(R.color.bg_gred_f1);
                ivLevelIcon.setImageResource(moduleExamBean.isExpanded() ? R.mipmap.ej_ejzk_ic :
                        R.mipmap.ej_erji_ic);
                topLine.setVisibility(View.VISIBLE);
                bottomLine.setVisibility(moduleExamBean.isLastIndex() && !moduleExamBean.isExpanded() ? View.INVISIBLE : View.VISIBLE);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (moduleExamBean.isExpanded()) {
                            collapse(pos, false);
                        } else {
                            expand(pos, false);
                        }
                    }
                });
                //标记子节点最后一个
                List<ModuleExamBean> sub2Items = moduleExamBean.getSubItems();
                if (sub2Items != null && !sub2Items.isEmpty()) {
                    sub2Items.get(sub2Items.size() - 1).setLastIndex(true);
                }
                break;
            case 2:
                helper.itemView.setBackgroundResource(R.color.bg_gred_f1);
                ivLevelIcon.setImageResource(R.mipmap.ej_sjk_ic);
                topLine.setVisibility(View.VISIBLE);
                //判断二级节点是否为最后一个节点
                ModuleExamBean parent2ModuleExamBean = (ModuleExamBean) getItem(getParentPosition(item));
                if (parent2ModuleExamBean != null) {
                    bottomLine.setVisibility(parent2ModuleExamBean.isLastIndex() && moduleExamBean.isLastIndex() ? View.INVISIBLE : View.VISIBLE);
                }
                break;
        }

    }
}
